package cc.rengu.igas.share.common.dao.impl;

import cc.rengu.igas.share.common.dao.RoleAcctInfoMapper;
import cc.rengu.igas.share.common.entity.RoleAcctInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author: wangcheng
 * @Date: 2020/7/4 11:38
 * @Description: 账户配置信息
 */
public class RoleAcctInfoMapperImpl implements RoleAcctInfoMapper {

    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public RoleAcctInfo selectBankRoleAcctInfoBySettleAcctId(String instId, String settleAcctId, String roleId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectShareBankRoleAcctInfoBySettleAcctId");
        RoleAcctInfo roleAcctInfo = new RoleAcctInfo();
        roleAcctInfo.setInstId(instId);
        roleAcctInfo.setSettleAcctId(settleAcctId);
        roleAcctInfo.setRoleId(roleId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, roleAcctInfo, roleAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("根据机构号<{}>，账号标识：{}，角色标识：{}获取账户配置信息失败", instId, settleAcctId, roleAcctInfo.getRoleId());
            return null;
        }
        return roleAcctInfo;
    }
}
